import { Location, LocationStrategy, PathLocationStrategy } from '@angular/common';
import { Component, OnInit } from '@angular/core';
// import { ToastService, UploadConfig } from 'ng-tui';
import { GlobalService, AuthService } from '../../../../cores/services';
import { ManageService } from '../../services/manage.service';
import { Manage } from '../../interfaces/manage.interface';
import { ActivatedRoute, Router } from '@angular/router';
import { Pagination, ConfirmService, ToastService, Item } from 'ng-tui';
import { ApiData, SearchParams } from '../../../../cores/classes';
import { Goods } from './../../interfaces/manage.interface';
import { skipWhile, switchMap } from 'rxjs/operators';

@Component({
    templateUrl: './account-manage.component.html',
    providers: [Location, { provide: LocationStrategy, useClass: PathLocationStrategy }],
})
export class AccountManageComponent implements OnInit {

    theads = ['#', '企业名称', '管理员', '手机号', '用户数', '客户数', '可用模块', '所在区域', '业务划归', '可用空间', '在线用户数', '合同查看', '状态', '操作'];
    onlineTheads = ['#', '用户名', '姓名', '手机号', '登录IP', '登录终端', '登录时长'];

    search = new SearchParams({ start: '', end: '' });

    list = new Array<Goods>();

    loading = false;

    pagination = new Pagination();
    parentTypes = new Array<Item>();
    childTypes = new Array<any>();
    goodsThreeType = new Array<any>();
    goods: Goods = { id: 0, };


    get getChildTypes(): Item[] {
        return this.childTypes.filter(type => type.parentId === this.goods.goodsParentType);
    }
    get getThreeTypes(): Item[] {
        return this.childTypes.filter(type => type.parentId === this.goods.goodsParentType);
    }
    constructor(
        private activatedRoute: ActivatedRoute,
        private router: Router,
        private confirm: ConfirmService,
        private toast: ToastService,
        public global: GlobalService,
        private manageService: ManageService
    ) { }

    ngOnInit() {
        this.activatedRoute.url
            .pipe(skipWhile(() => this.router.url !== '/manage/account'))
            .subscribe(() => this.loadDatas());
    }

    doSearch() {
        this.pagination.reset();
        this.loadDatas();
    }

    doReset() {
        this.search.clean();
        this.doSearch();
    }

    loadDatas() {
        this.loading = true;
        this.manageService.searchcity(this.pagination, this.search)
            .subscribe({
                next: res => {
                    this.pagination.total = res.datas.total;
                    this.list = res.datas.rows;
                },
                complete: () => this.loading = false
            });
    }

    confirmDelete(id: number, status: number) {
        this.confirm.danger('删除确认', `您确认删除企业吗？`)
            .pipe(switchMap<void, ApiData>(() => this.manageService.deletecity(id, status)))
            .subscribe(() => {
                this.list.splice(this.list.indexOf(this.goods), 1);
                this.toast.success('删除成功', `成功删除企业为`);
                this.loadDatas();
            });
    }
    /**变更状态
     *
     */
    // change(item) {
    //     this.personnelService.updateBannerdd(item.id, item.isActive).subscribe({
    //         next: res => {
    //             if (res.result) {
    //                 this.toast.success('更改成功', `状态修改成功`);
    //                 this.loadDatas();
    //             }
    //         }
    //     });

    // }

}
